package com.distribution.system.manager.channel.channelInfo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.distribution.system.domain.bo.CmccDaUpChannelInfoBO;
import com.distribution.system.domain.entity.CmccDaUpChannelInfo;
import com.distribution.system.mapper.CmccDaUpChannelInfoMapper;
import com.distribution.system.utils.convert.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 三方信息管理器
 *
 * @author destinyhlp
 * @date 2023/6/1 21:46
 */

@Service
public class ChannelInfoManager {
    /**
     * 三方信息缓存
     */
    private final ConcurrentHashMap<Integer, CmccDaUpChannelInfoBO> channelInfoCaChe = new ConcurrentHashMap<>(1 << 4);

    private final CmccDaUpChannelInfoMapper cmccDaUpChannelInfoMapper;

    public ChannelInfoManager(CmccDaUpChannelInfoMapper cmccDaUpChannelInfoMapper) {
        this.cmccDaUpChannelInfoMapper = cmccDaUpChannelInfoMapper;
    }

    @PostConstruct
    public void channelInfoCaChe() {
        refreshCache();
    }

    /**
     * 根据三方id 获取三方信息
     */
    public CmccDaUpChannelInfoBO getChannelInfoById(Integer tripartiteId) {
        return channelInfoCaChe.get(tripartiteId);
    }

    /**
     * 刷新三方信息缓存
     */
    public void refreshCache() {
        channelInfoCaChe.clear();
        List<CmccDaUpChannelInfo> cmccDaUpChannelInfos = cmccDaUpChannelInfoMapper.selectList(new QueryWrapper<>());
        List<CmccDaUpChannelInfoBO> cmccDaUpChannelInfoBos = BeanUtils.copyBeanCollection(CmccDaUpChannelInfoBO.class, cmccDaUpChannelInfos);
        for (CmccDaUpChannelInfoBO cmccDaUpChannelInfoBo : cmccDaUpChannelInfoBos) {
            channelInfoCaChe.put(cmccDaUpChannelInfoBo.getChannelId(), cmccDaUpChannelInfoBo);
        }
    }
}
